﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="PageTitle" runat="server">
    <title>工作日志管理</title>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="PageStyles" runat="server">
    <link href="../../Content/fullcalendar.css" rel="stylesheet" type="text/css" />
    <link href="../../Content/TemplateCSS.css" rel="stylesheet" type="text/css" />
    <link href="../../Content/project/project.css" rel="stylesheet" type="text/css" />
    <link href="../../Content/ui.fullcalendar.css" rel="Stylesheet" type="text/css" />
    <style type="text/css">
        .txt-dailyinfo
        {
            font-weight: bolder;
        }
        .liWrokitem
        {
            font-size: 14px;
            list-style-type: disc;
            list-style-position: outside;
            margin-left: 30px;
        }
        .liWrokitem a
        {
            display: inline;
        }
    </style>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="PageScripts" runat="server">
    <script src="../../Scripts/fullcalendar.js" type="text/javascript"></script>
    <script src="../../Scripts/jquery-jtemplates.js" type="text/javascript"></script>
    <script type="text/javascript">


    var URL_PERSONAL_DAILY_WORK_LIST = '/project/DailyWorkListPerMonth';
    var URL_SIMPLEUSERINFO_BY_DEPTID = '/core/GetUserInfoInDept';

    //************* 常量定义



    var StaticResource = {
       
    };

    var Super = {
        Calendar: null,
        TipAttendance: null,
        MemberSelector: null,

        //data
        InfoForCalendarList: null,
        WorkitemPerDayList: null,
        CurrentDate: null,
        TargetDate: null,
        _self: null,
        _isBinding: false,
        internalInit: function()
        {
            Super.Calendar = $('#calendar');
            Super.MemberSelector = 
            Super.pageInit();
            Super.dataInit();

        },

        pageInit:function()
        {
           $('#tip_Remind').eztips(); 
        },

        dataInit:function()
        {
            var currentUser = EzAccessor.getCurrentUser();
            var date= Date.fromEpoch(EzAccessor.getCurrentTime());
            
            Super.updateTitleInfo(currentUser);

            Super.CurrentDate =  new Date(
			    date.getFullYear(),
			    date.getMonth() + 1,
			    0,
			    0,
			    0,
                0
            ).toEpoch();
            
            //calendar
            $('#calendar').fullCalendar({ 
                theme: true,
                header: {
                    left: 'prev,next today',
                    center: 'title',
                    right: ''
                },
                editable: false,events: null
            });
            EzAccessor.doUpdateDailyWorkList(
                    Super.CurrentDate,
                    Super.bindCalendarInfoList);
        },

        bindCalendarInfoList:function (e) {
            Super.InfoForCalendarList = e[0];
            Super.WorkitemPerDayList= e[1];
            
            var dailyWorkList = 
            DataConverterForfullcalendar.convertfullcalendar(Super.InfoForCalendarList, convertor);

            Super._isBinding = true;
            $('#calendar').empty();
            $('#calendar').fullCalendar({
                theme: true,
                header: {
                    left: 'prev,next today',
                    center: 'title',
                    right: ''
                },
                editable: false,
                events: dailyWorkList,
                viewDisplay: Super.viewChangedCallback,
                eventClick: Super.mouseClickEventCallback,
                windowResize: Super.windowResizeCallback,
                eventAfterRender: Super.eventAfterRenderCallback
            });        
            $('#calendar').fullCalendar('gotoDate',
                                         Date.fromEpoch(Super.CurrentDate).getFullYear(),
                                         Date.fromEpoch(Super.CurrentDate).getMonth()
                                        );            
            
            var height = $('.fc-grid table').height();
            adjustEventElementHeight(height);

            Super._isBinding = false;
        },
        viewChangedCallback:function (e){
            var calendarDate = ($('#calendar').fullCalendar('getDate'));
           
            var date = new Date(
                calendarDate.getFullYear(),
				calendarDate.getMonth() + 1,
                0,
                0,
                0,
                0
            ).toEpoch();

            if(date != Super.CurrentDate && !Super._isBinding){
                Super.CurrentDate = date;
                
                EzAccessor.doUpdateDailyWorkList(
                    Super.CurrentDate,
                    Super.bindCalendarInfoList);
            };

        },

        mouseClickEventCallback:function (eventData) {
            //清空原来的数据
            cleanTipData();
            $('#tip_DailyInfo').eztips();
            //存储日期
            var date = eventData.id;
            Super.TargetDate = date;
            //更新时间标题
            var dateTime = Date.fromEpoch(date).toFullDate();
            $('#txtTitle').html(dateTime + "&nbsp;" + "工作日志:");
            
            var workItemList = getWorkitemListTheDay(date);
            bindWorkItemList(workItemList.workitemList);
            bindDailyWork(workItemList.dailyWork);
            
            $('#alinkView').click(function () {
                window.location.href = "/project/DailyWork?time=" + Super.TargetDate;
            });
        },
        
        windowResizeCallback:function (view){
            var tableHeight = view.element[0].clientHeight;
            adjustEventElementHeight(tableHeight)
        },
        
        eventAfterRenderCallback :function (eventData, jsEvent, view){
            var date = eventData.id;
            var workitemListTheDay = getWorkitemListTheDay(date); 
            var _self=  view.eventElementsByID[eventData.id][0];
            //改变背景颜色
            var color = $(_self).find('a').css( 'background-color' );
            $(_self).hover(function (){
                $(_self).css( {'background-color': '#dadada'} );
                $(_self).find('a').css( {'background-color': '#dadada'} );
                },
                function (){
                    $(_self).css( {'background-color': color} );
                    $(_self).find('a').css( {'background-color': color} );
                });

            //qtips 
            if(workitemListTheDay.workitemList.length > 0){  
                $(_self).qtip({
                    content: function () {
                        //todo:获取数据
                        return getWorkInfoTheDay(workitemListTheDay);
                    },
                    position: {
                        my: 'left center',  
                        at: 'right center' 
                    }
                });
            }
        },

        updateTitleInfo :function(currentUser) {
            var title = currentUser.Name + "的工作日志";
            
            $('#txtDailyWorkTitle').html(title);
        },
    
   };
    


    //************* URL常量




    var convertor = {

        "id": function (item) {
            return item.Date;
        },
        "title": function (item) {
            return item.Title;
            
        },
        "start": function (item) {
            return item.Date;

        },

        "allDay": function (item) {
            return true;
        },

        "className": function (item) {
            if (Date.fromEpoch(item.Date).toFullDate() == new Date().toFullDate()){
                return 'fc-event-today';
            }
            return 'fc-dailyWork'; 
        }

    };



    //************* Accessor访问器

    var EzAccessor = {};

    if (ezmock.IS_MOCKED) {
        var mock_isHasPermisson = true;
        var mockEzAccessor = {
        };
        EzAccessor = mockEzAccessor;
    } else {
        var realEzAccessor = {
            'doUpdateDailyWorkList': function (time, callback) {
                $(document).ajaxBusy('begin');
                //清空数据
                $('#calendar').fullCalendar('removeEvents');
                $.getJSON(
                    URL_PERSONAL_DAILY_WORK_LIST,
                    { 'time': time },function(e){
                        callback(e);
                        $(document).ajaxBusy('end');
                    });
            },
            'getCurrentUser': function () {
                return <%= WebJsonExtension.ToJson(Model.CurrentUser) %>; 
            },
            
            'getCurrentTime': function () {
                return <%= WebJsonExtension.ToJson(Model.CurrentTime) %>; 
            }
        };

        EzAccessor = realEzAccessor;
    }

    ///adjustEventElementHeight
    function adjustEventElementHeight(tableHeight) {
       var elementHeight = (((tableHeight / 6) * 0.90) - 25) + 'px';

       $('.fc-event').each(function() {
            $(this).css({ 'height': elementHeight});
       });
    }


    function getWorkInfoTheDay(workitemListTheDay) {
        var totalPoints = '<p>完成了 {Points} 项任务 </p>';
        totalPoints = totalPoints.replace('{Points}', workitemListTheDay.workitemList.length);
        var totalWorkItem = '<p>一共花费了 {Count} 个工时</p>';
        totalWorkItem  = totalWorkItem.replace('{Count}', workitemListTheDay.totalPoints);   
        
        return totalPoints + totalWorkItem; 
    }

   
    function getWorkitemListTheDay(date) {
        var workitemListTheDay = null;
        for(var key in Super.WorkitemPerDayList){
            //todo:
            if(Super.WorkitemPerDayList[key].theDay == date){
                workitemListTheDay = Super.WorkitemPerDayList[key];
            }
        }

        return workitemListTheDay;
    }

    function bindWorkItemList (workitemList) {
        if(workitemList.length > 0) {
            for (var key in workitemList){
                var workitemDom= $('<li class="liWrokitem"></li>');
                
                var workitemInfoStr = '<a class="link-workitem" href="/project/workitemList?workitemId={Id}&&isFinished=true">{Name}</a>';
                workitemInfoStr =  workitemInfoStr
                  .replace('{Name}', workitemList[key].Title)
                  .replace('{Id}', workitemList[key].Id);
                var workitemInfoDom = $(workitemInfoStr)
                    .appendTo(workitemDom);
                
                if (workitemList[key].Project != null){
                    var projectStr = '<a class="link-project" href="/project/projectInfo/{Id}"> {ProjectName} </a>';
                    projectStr =  projectStr
                        .replace('{ProjectName}', workitemList[key].Project.Name)
                        .replace('{Id}', workitemList[key].Project.Id);
                    var workitemProjectInfoDom = $(projectStr)
                        .appendTo(workitemDom);
                }

                $('#ulDailyInfo').append(workitemDom);
            }
        }
        else {
            $('#ulDailyInfo').append('<li>没有完成的任务</li>')
        }
    }
  
    function bindDailyWork(dailyWork) {
        $('#txtDailyWork').html(dailyWork);
    }


    function cleanTipData() {
        $('#txtTitle').empty();
        $('#ulDailyInfo').empty();
        $('#txtDailyWork').empty();
    };
    
    $(function () {
        Super.internalInit();
    });


    </script>
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="PageHead" runat="server">
    <%
        Html.RenderPartial("_sayHello"); %>
</asp:Content>
<asp:Content ID="Content5" ContentPlaceHolderID="PageContent" runat="server">
    <div id="page-head" class="page-head">
        <img src="../../Content/icons/calendar_red.png" style="width: 48px; height: 48px;"
            alt="attendanceInfo" />
        <span id="txtDailyWorkTitle"  class="title"></span>
    </div>
    <div id="page-body">
        <div id="calendar" style="width: 100%;">
        </div>
    </div>
</asp:Content>
<asp:Content ID="Content6" ContentPlaceHolderID="PageShortcut" runat="server">
    <div id="shortcuts" class="box">
        <a id="btn-dailywork" href="/project/DailyWork">今天的工作日志</a>
                <script type="text/javascript">
                    $(function () {
                        $("#btn-dailywork").button({
                            icons: {
                                primary: "ui-icon-circle-plus"
                            },
                            text: true
                        });
                    });
        </script>
    </div>
</asp:Content>
<asp:Content ID="Content7" ContentPlaceHolderID="PageFoot" runat="server">
    <div id='tip_Remind' style="display: none;">
        <h1>
            小贴士:</h1>
        <p class="indent">
        </p>
    </div>
    <div id="tip_DailyInfo" style="display: none;" class="tip-attention">
        <h1 id="txtTitle">
        </h1>
        <div class="indent">
            <p class="txt-dailyinfo">
                完成任务 :
            </p>
            <ul id="ulDailyInfo">
            </ul>
            <p class="txt-dailyinfo">
                日志 :
            </p>
            <p id="txtDailyWork" style="margin-left: 8px;">
            </p>
            <p>
                <a id="alinkView">详细..</a>
            </p>
        </div>
    </div>
</asp:Content>
